package com.controller;

import com.entity.Dept;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
	@Autowired
	private DeptService service;
	// @RequestBody
	// 如果参数是放在请求体中，传入后台的话，那么后台要用@RequestBody才能接收到
	@PostMapping("/add")
	public boolean addDept(@RequestBody Dept dept) {
	return service.addDept(dept);
	}
	@GetMapping("/get/{id}")
	@HystrixCommand(fallbackMethod = "getHystrix")
	public Dept get(@PathVariable(value = "id") Long id) {
		Dept dept = service.queryById(id);
		if (null== dept ){
			throw new RuntimeException("数据不存在就跳转到hystrxi定义的方法中");
		}
		return dept;
	}
	
	public Dept getHystrix(@PathVariable(value = "id")Long id) {
		return new Dept().setDeptno(id).setDname("bug 了 404 中....");
	}
	
	@GetMapping("/list")
	public List<Dept> queryAll() {
	return service.queryAll();
	}
}
